package com.lagou.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class HbaseClient {
    private Configuration conf;
    private Connection connection;

    public HbaseClient() {
        synchronized (HbaseClient.class) {
            if (null == conf) {
                init();
            }
        }
    }

    private void init() {
        this.conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "centos1,centos2,centos3");
        conf.set("hbase.zookeeper.property.clientPort", "2181");
        try {
            connection = ConnectionFactory.createConnection(conf);
            System.out.println("hbase 链接创建完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void close() {
        if (null != connection) {
            try {
                connection.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public Connection getConnection() {
        return connection;
    }
}
